// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.expand.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.common.ImportResp;
import com.liuxinlong.common.UploadService;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.Inventory;
import com.liuxinlong.modules.expand.controller.request.ImportInventoryReq;
import com.liuxinlong.modules.expand.controller.request.QueryInventoryReq;
import com.liuxinlong.modules.expand.controller.request.UpdateInventoryReq;
import com.liuxinlong.modules.expand.service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 库存管理控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022年5月20日
 */
@RestController
@RequestMapping("/inventory")
public class InventoryController {
    @Autowired
    private InventoryService inventoryService;

    @Autowired
    private UploadService uploadService;

    /**
     * 分页查询库存信息
     *
     * @param req 请求信息
     * @return 库存信息列表
     */
    @PostMapping("page")
//    @LogAnnotation(value = "分页查询", business = LogBusinessConstants.INVENTORY_MANAGE,operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp pageInfo(@RequestBody QueryInventoryReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("sn", req.getSn());
        queryParam.put("name", req.getName());
        queryParam.put("startTime", req.getStartTime());
        queryParam.put("endTime", req.getEndTime());
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        List<Map<String, Object>> dataList = inventoryService.pageInventoryList(queryParam);
        int totalNum = inventoryService.getInventoryCount(queryParam);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询库存信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 新增库存信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("add")
    @LogAnnotation(value = "新增库存信息", business = LogBusinessConstants.INVENTORY_MANAGE,operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addInventoryInfo(@RequestBody UpdateInventoryReq req) {
        Inventory inventory = new Inventory();
        inventory.setSn(req.getSn());
        inventory.setName(req.getName());
        inventory.setUnit(req.getUnit());
        inventory.setPurchaseNumber(req.getPurchaseNumber());
        inventory.setLeftNumber(req.getLeftNumber());
        inventory.setStatus(req.getStatus());
        inventory.setPurchaseTime(req.getPurchaseTime());
        inventory.setExpireTime(req.getExpireTime());
        inventory.setRemark(req.getRemark());
        inventoryService.addInventory(inventory);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增库存信息成功");
        return resp;
    }

    /**
     * 修改库存信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update")
    @LogAnnotation(value = "修改库存信息", business = LogBusinessConstants.INVENTORY_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateInventoryInfo(@RequestBody UpdateInventoryReq req) {
        Inventory inventory = new Inventory();
        inventory.setId(req.getId());
        inventory.setSn(req.getSn());
        inventory.setName(req.getName());
        inventory.setUnit(req.getUnit());
        inventory.setPurchaseNumber(req.getPurchaseNumber());
        inventory.setLeftNumber(req.getLeftNumber());
        inventory.setStatus(req.getStatus());
        inventory.setPurchaseTime(req.getPurchaseTime());
        inventory.setExpireTime(req.getExpireTime());
        inventory.setRemark(req.getRemark());
        inventoryService.updateInventory(inventory);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改库存信息成功");
        return resp;
    }

    /**
     * 删除库存信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value = "删除库存信息", business = LogBusinessConstants.INVENTORY_MANAGE,operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteInventoryInfo(@RequestBody BaseDeleteReq req) {
        inventoryService.deleteInventory(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除库存信息成功");
        return resp;
    }

    /**
     * 导入数据预处理
     *
     * @param multipartRequest 文件
     * @return 数据
     */
    @PostMapping("import")
    @LogAnnotation(value = "导入库存信息数据", business = LogBusinessConstants.INVENTORY_MANAGE,operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp importInventory(MultipartRequest multipartRequest) {
        Map<String, Object> map = uploadService.uploadFileLocal(multipartRequest);
        ImportResp resp = inventoryService.importInventory((String) map.get("fileRealPath"));
        return resp;
    }

    /**
     * 导入库存信息
     *
     * @param req 请求体
     * @return 返回导入失败数据
     */
    @PostMapping("batch-add")
    @LogAnnotation(value = "导入库存信息数据", business = LogBusinessConstants.INVENTORY_MANAGE,operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp batchAddInventory(@RequestBody ImportInventoryReq req) {
        return inventoryService.batchAddInventory(req.getDataList());
    }

    /**
     * 导出库存信息
     *
     * @param req      请求体
     * @param response 返回体
     */
    @PostMapping("export")
    @LogAnnotation(value = "导出库存信息", business = LogBusinessConstants.INVENTORY_MANAGE,operateType = LogOperateConstants.OPERATE_EXPORT)
    public void exportSpecificationInfo(@RequestBody QueryInventoryReq req, HttpServletResponse response) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("sn", req.getSn());
        queryParam.put("name", req.getName());
        queryParam.put("startTime", req.getStartTime());
        queryParam.put("endTime", req.getEndTime());
        inventoryService.exportInventoryInfo(queryParam, response);
    }

    /**
     * 清空库存信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("clear")
    @LogAnnotation(value = "清空库存信息", business = LogBusinessConstants.INVENTORY_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp clearInventoryInfo(@RequestBody BaseDeleteReq req) {
        inventoryService.clearInventory(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("清空库存信息成功");
        return resp;
    }

    /**
     * 批量清空库存信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("clear-batch")
    @LogAnnotation(value = "批量清空库存信息", business = LogBusinessConstants.INVENTORY_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp batchClearInventoryInfo(@RequestBody BaseDeleteReq req) {
        inventoryService.batchClearInventory(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("批量清空库存信息成功");
        return resp;
    }

}
